package com.atguigu.system.handler;

import com.atguigu.common.result.Result;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.system.exception.GuiguException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 全局的异常处理器
 */
@ResponseBody
@ControllerAdvice
public class GlobleExceptionHandler {

    //处理异常的方法
    @ExceptionHandler(Exception.class)
    public Result resolveException(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    //创建一个处理数学异常的方法
    @ExceptionHandler(ArithmeticException.class)
    public Result resolveArithmeticException(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().code(444).message("出现了数学异常！");
    }

    //创建一个处理自定义异常的方法
    @ExceptionHandler(GuiguException.class)
    public Result resolveGuiguException(GuiguException e){
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }

    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }

}
